﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Psm.PubSub.StateCache;
using Psm.PubSub.Messaging;
using Psm.Container;
using System.Diagnostics;
using Psm.Util;

namespace PubSubTests
{
    [TestClass]
    public class CacheTests
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [ClassInitialize]
        public static void ClassSetup(TestContext tc)
        {
            Repository.Initialize();
        }

        [TestMethod]
        public void TopicSpacePerformance()
        {
            TopicSpace topicSpace = new TopicSpace(SchemaCollection.AllSchemas["TOPIC1"]);
            int MESSAGE_COUNT = 1000000;
            Queue<DataMessageInternal> messageQueue = new Queue<DataMessageInternal>(MESSAGE_COUNT);
            TestContext.WriteLine("Prepareing Messages...");
            for (int index = 0; index < MESSAGE_COUNT; index++)
            {
                DataMessageInternal message = new DataMessageInternal(SchemaCollection.AllSchemas["TOPIC1"]);
                message.AddData("00001", ByteBuffer.StringToByteBuffer((index + 1).ToString()));
                message.AddData("00002", ByteBuffer.StringToByteBuffer("00000002"));
                message.AddData("00003", ByteBuffer.StringToByteBuffer("00000003"));
                message.AddData("00004", ByteBuffer.StringToByteBuffer("00000004"));
                message.AddData("00005", ByteBuffer.StringToByteBuffer("00000005"));
                message.AddData("00006", ByteBuffer.StringToByteBuffer("00000006"));
                message.AddData("00007", ByteBuffer.StringToByteBuffer("00000007"));
                message.AddData("00008", ByteBuffer.StringToByteBuffer("00000008"));
                message.AddData("00009", ByteBuffer.StringToByteBuffer("00000009"));
                message.AddData("00010", ByteBuffer.StringToByteBuffer("00000010"));
                messageQueue.Enqueue(message);
            }
            TestContext.WriteLine("Prepareing Messages Done.");
            TestContext.WriteLine("Adding to topic space");
            Stopwatch stopwatch = Stopwatch.StartNew();
            while (messageQueue.Count > 0)
                topicSpace.AddMessage(messageQueue.Dequeue());
            stopwatch.Stop();
            TestContext.WriteLine("{0:n} messages added in {1} ms", MESSAGE_COUNT, stopwatch.ElapsedMilliseconds);
            TestContext.WriteLine("{0:n} messages added in 1 second", (double)MESSAGE_COUNT / (double)stopwatch.ElapsedMilliseconds * 1000.00);
        }
    }
}
